﻿namespace BM.Tools.WebControls.MForm.Controls.Titles
{
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    /// <summary>
    /// A control that displays title
    /// </summary>
    public class TitleControl : CompositeControl, ITextControl
    {
        private const string TitleCssClass = "MForm-Title";

        /// <summary>
        /// Initializes a new instance of the <see cref="TitleControl"/> class.
        /// </summary>
        public TitleControl() : this(null)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="TitleControl"/> class.
        /// </summary>
        /// <param name="titleText">
        /// The title text.
        /// </param>
        public TitleControl(string titleText)
        {
            Initialize(titleText);
        }

        /// <summary>
        /// Gets or sets Text.
        /// </summary>
        /// <value>
        /// The text of the title.
        /// </value>
        public virtual string Text
        {
            get;
            set;
        }

        /// <summary>
        /// The div key.
        /// </summary>
        /// <value>
        /// The tag key.
        /// </value>
        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Div;
            }
        }

        /// <summary>
        /// renders the inner "Content" div
        /// </summary>
        /// <param name="writer">
        /// The writer.
        /// </param>
        protected override void RenderContents(HtmlTextWriter writer)
        {
            writer.Write(@"<div class=""MForm-Content"">");
            if (!String.IsNullOrEmpty(Text))
            {
                writer.Write(Text);
            }

            base.RenderContents(writer);
            writer.Write("</div>");
        }

        /// <summary>
        /// initialize the title with the specifed text
        /// </summary>
        /// <param name="titleText">
        /// The title text.
        /// </param>
        private void Initialize(string titleText)
        {
            Text = titleText;
            CssClass = TitleCssClass;
        }
    }
}